package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

@Slf4j
public class GodaddyCommonsEmailSender {

    private static final String smtpHost = "smtpout.secureserver.net";
    private static final int smtpPort = 465;
//    private static final String username = "team@eastcard.io";
//    private static final String password = "mB49H=fyyb";
    private static final String username = "service@ubpay.ai";
    private static final String password = "2bvW5p9+G8";

//    public static void sendEmailUsingCommons(String toAddress, String subject, String content) throws EmailException {
//        HtmlEmail email = new HtmlEmail();
//        String imagePath = "images/logo.png";
//        String cid = email.embed(imagePath, "公司Logo");
//        //主机名
//        email.setHostName(smtpHost);
//        //端口
//        email.setSmtpPort(smtpPort);
//        //账号以及密码
//        email.setAuthenticator(new DefaultAuthenticator(username, password));
//        //开启ssl连接
//        email.setSSLOnConnect(true);
//        //目的邮件
//        email.addTo(toAddress);
//        //发送者
//        email.setFrom(username, "UBPay");
//        //标题
//        email.setSubject(subject);
//        //邮件内容
//        email.setHtmlMsg(content);
//        email.setCharset("UTF-8");
//        email.send();
//    }

    public static void sendCode(String receiverEmail, String code) throws EmailException {
        HtmlEmail email = new HtmlEmail();
        String imagePath = "http://api.ubpay.ai/infra/file/1747439750615781378/get/cw-interlace/normal/7383f88bc15fdd7de1f0ea09eb9b186b2d829e9111397852cda7ddc160049a79.png";
        String cid = email.embed(imagePath, "logo");
        //主机名
        email.setHostName(smtpHost);
        //端口
        email.setSmtpPort(smtpPort);
        //账号以及密码
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        //开启ssl连接
        email.setSSLOnConnect(true);
        String emailSubject = "Your UBPay Verification Code";
        String emailContent =
            "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <style type=\"text/css\">\n" +
                "        @media screen and (max-width: 600px) {\n" +
                "            .container { width: 100%!important; }\n" +
                "            .content { padding: 15px!important; }\n" +
                "        }\n" +
                "        .hidden-content { display: none; }\n" +
                "        .toggle-checkbox { display: none; }\n" +
                "        .toggle-checkbox:checked + .hidden-content { display: block; }\n" +
                "        .toggle-checkbox:checked ~ .content { display: none; }\n" +
                "        .toggle-button {\n" +
                "            color: blue;\n" +
                "            text-decoration: underline;\n" +
                "            text-align: center;\n" +
                "            display: inline-block;\n" +
                "            cursor: pointer;\n" +
                "        }\n" +
                "        .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .full-content .hidden-content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content .hidden-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .content {\n" +
                "            height: 400px;\n" +
                "            .position {\n" +
                "                padding: 200px 0;\n" +
                "            }\n" +
                "        }\n" +
                "        .container {\n" +
                "            display: flex;\n" +
                "            flex-direction: column;\n" +
                "            justify-content: center;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <input type=\"checkbox\" id=\"toggle-1\" class=\"toggle-checkbox\">\n" +
                "        <div class=\"content\">\n" +
                "            <div class=\"position\">\n" +
                "                <p>...</p>\n" +
                "                <span>[邮件部分隐藏]</span>\n" +
                "                <!-- 展开按钮 -->\n" +
                "                <label for=\"toggle-1\" class=\"toggle-button\">点击展开内容</label>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "        <div class=\"full-content\">\n" +
                "            <!-- 隐藏内容 -->\n" +
                "            <div class=\"hidden-content\">\n" +
                "\t\t\t\t<img src='cid:" + cid + "' width='70' alt='logo' />\n" +
                "                <p><strong style=\"font-size: 18px;\">Dear Valued Customer,</strong></p>\n" +
                "                <p>Your verification code is: <strong style=\"font-size: 22px;\">" + code + "</strong></p>\n" +
                "                <p><br></p>\n" +
                "                <p>This code will expire within 5 minutes. Never provide this code to anyone.\n" +
                "\t\t\t\tIf you have any question or need support, please contact our customer service team immediately.</p>\n" +
                "                <p><br></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">Best regards,</strong></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">The UBPay Team</strong></p>\n" +
                "\t\t\t\t<p style=\"font-size: 16px;font-style: italic;\">This is an automated message, please do not reply.</p>\n" +
                "                <p style=\"font-size: 14px;\">© 2025 UBPay, All Rights Reserved.</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>\n";
        try {

            //目的邮件
            email.addTo(receiverEmail);
            //发送者
            email.setFrom(username, "UBPay");
            //标题
            email.setSubject(emailSubject);
            //邮件内容
            email.setHtmlMsg(emailContent);
            email.setCharset("UTF-8");
            email.send();
        } catch (EmailException ex) {
            log.info("邮件发送失败:{}", ex.getMessage());
        }
    }

    public static void main(String[] args) throws EmailException {
//        sendCode("1902339614@qq.com", "147963");
        sendCode("764380296@qq.com", "147963");
    }
}
